package com.alex.algorithm.easy;

public class BestTimeToBuyAndSell {

    public static void main(String[] args) {
        int[] prices = {7,1,5,3,6,10};
        System.out.println(getProfit(prices));
    }

    private static int getProfit(int[] prices) {
        int min = prices[0];
        int profit = 0;
        for (int i  = 1; i < prices.length; ++i) {
            if (prices[i] > prices[i -1]) {
                profit = Math.max(profit, prices[i] - min);
            }else {
                min = Math.min(min, prices[i]);
            }
        }
        return profit;
    }
}
